package cn.mdmm.exception;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver;

import com.alibaba.fastjson.JSON;

import cn.modoumama.common.exception.BaseException;
import cn.modoumama.common.response.Response;

public class DefaultExceptionResolver extends AbstractHandlerExceptionResolver {

	Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Override
	protected ModelAndView doResolveException(HttpServletRequest request,HttpServletResponse response, Object handler, Exception ex) {
		String code = null;
		String message = null;
		if(ex instanceof BaseException){
			code = ((BaseException)ex).getCode();
			message = ex.getMessage();
			Response responseBody = new Response();
			responseBody.addErrInfo(code, message, null);
			return new ModelAndView("json").addObject("json", JSON.toJSON(responseBody));
		}else{
			logger.error("ExceptionResolver拦截到异常", ex);
			request.setAttribute("exception", ex);
			return new ModelAndView("ControllerException");
		}
	}

	

}
